package values.npcs
{
	import values.avatar.AvatarTemplateValue;
	import values.avatar.AvatarTemplateValueList;

	public class NpcTemplateValue
	{
		[Data(type="field" , mapping="@key")]
		public var key:String;
		
		[Data(type="field" , mapping="@avatar_key")]
		public var avatarKey:String;
		
		private var _params:*;

		public function get params():*
		{
			return _params;
		}
		
		public function get template():AvatarTemplateValue
		{
			return AvatarTemplateValueList.getByKey(avatarKey);
		}
		
		[Data(type="field" , mapping="params")]
		public function set params(value:*):void
		{
			_params = {};
			if(value.level && value.level != "")
			{
				_params.level = int(value.key);
			}
			else
			{
				_params.level = 1;
			}
			
			if(value.xp && value.xp != "")
			{
				_params.xp = int(value.xp);
			}
			else
			{
				_params.xp = 1;
			}
			
			if(value.enhance)
			{
				_params.enhance = [int(value.enhance.normal) , int(value.enhance.skill) , int(value.enhance.element)];
			}
			else
			{
				_params.enhance = [0 , 0 , 0];
			}
			
			if(value.unlockedSlots)
			{
				_params.unlockedSlots = int(value.unlockedSlots);
			}
			else
			{
				_params.unlockedSlots = 0;
			}
			
			if(value.skills)
			{
				_params.skills = [];
				for each(var v:XML in value.skills)
				{
					_params.skills.push([String(v.@key) , int(v.@level)]);
				}
			}
			else
			{
				_params.skills = [[template.defaultSkill.key , 1]];
			}
			
			if(value.ultimateSkillLevel)
			{
				_params.ultimateSkillLevel = int(value.ultimateSkillLevel);
			}
			else
			{
				_params.ultimateSkillLevel = 0;
			}
			
			if(value.selectedSkill)
			{
				_params.selectedSkill = String(value.selectedSkill);
			}
			else
			{
				_params.selectedSkill = template.defaultSkill.key;
			}
			
			if(value.reward)
			{
				_params.reward = [int(value.reward.normal) , int(value.reward.skill) , int(value.reward.element)];
			}
			else
			{
				_params.reward = [0 , 0 , 0];
			}
			
			if(value.equipments)
			{
				_params.equipments = [];
				for each(var v2:XML in value.equipments)
				{
					_params.equipments.push(["" , String(v2.@key) , null]);
				}
			}
			else
			{
				_params.equipments = [];
			}
		}

	}
}